package cool.tianyou.blue.bird.model;

import android.graphics.drawable.Icon;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.alibaba.fastjson.JSONObject;

import cool.tianyou.blue.bird.constant.MessageTypeEnum;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Data
public class Log {
    private MessageTypeEnum type;
    private List<String> pushedDevices;

    public void setType(MessageTypeEnum type) {
        this.type = type;
    }

    public void setPushedDevices(List<String> pushedDevices) {
        this.pushedDevices = pushedDevices;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public static void setMap(Map<String, MessageTypeEnum> map) {
        Log.map = map;
    }

    public void setIcon(Icon icon) {
        this.icon = icon;
    }

    private String content;

    private Boolean saveToLogList = true;
    private Date time;
    public static Map<String, MessageTypeEnum> map = new HashMap<>();
    private Icon icon;
    private String pkgName;

    public Log(NotificationData notificationData, Date time, Boolean saveToLogList) {
        this.pkgName = notificationData.getPkgName();
        this.content = notificationData.getContent();
        this.icon = notificationData.getIcon();
        this.time = time;
        this.saveToLogList = saveToLogList;


    }

    public void setPkgName(String pkgName) {
        this.pkgName = pkgName;
    }

    public Icon getIcon() {
        return icon;
    }

    @Override
    public String toString() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA);
        return String.format(Locale.CHINA, "[%s] 设备:%s", dateFormat.format(this.getTime()), this.pushedDevices==null?"正在推送":this.pushedDevices.size());
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static Log ofSMSLog(String content, String pushResult, Icon icon) {
        List<String> alias = JSONObject.parseObject(pushResult).getJSONArray("alias").toJavaList(String.class);
        return new Log(MessageTypeEnum.SMS, alias, content, icon);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static Log createLog(String content, String type, String pushResult, Icon icon, String pkgName) {
        Log log = null;

        if (type.equals("sms")) {
            log = ofSMSLog(content, pushResult, icon);
        }
        if (type.equals("clipBoard")) {
            log = ofClipBoardLog(content, pushResult);
        }
        if (type.equals("batteryLevel")) {
            log = ofBatteryLog(content, pushResult);
        }
        if (type.equals("notification")) {
            log = ofNotification(content, pushResult, icon);
        }
        if (log != null) {
            log.setPkgName(pkgName);
        }
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private static Log ofNotification(String content, String pushResult, Icon icon) {
        List<String> alias = JSONObject.parseObject(pushResult).getJSONArray("alias").toJavaList(String.class);
        return new Log(MessageTypeEnum.BATTERY, alias, content, icon);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private static Log ofBatteryLog(String content, String pushResult) {
        List<String> alias = JSONObject.parseObject(pushResult).getJSONArray("alias").toJavaList(String.class);
        return new Log(MessageTypeEnum.BATTERY, alias, content, null);
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    public static Log ofClipBoardLog(String content, String pushResult) {
        List<String> alias = JSONObject.parseObject(pushResult).getJSONArray("alias").toJavaList(String.class);
        return new Log(MessageTypeEnum.CLIP_BOARD, alias, content, null);
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    public Log(MessageTypeEnum type, List<String> pushedDevices, String content, Icon icon) {
        this.type = type;
        this.pushedDevices = pushedDevices;
        this.content = content;
        this.time = new Date();
        this.icon = icon;
    }

    public Log() {
    }

    public MessageTypeEnum getType() {
        return type;
    }

    public List<String> getPushedDevices() {
        return pushedDevices;
    }

    public String getContent() {
        return content;
    }

    public Date getTime() {
        return time;
    }
}
